﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SAServices.DAL
{
    class GenDAL
    {
        internal static DataTable GetTable(Model.ConnectionConext connCtx, string sql)
        {
            DataTable retTable = new DataTable();

            SqlConnection conn = connCtx.GetOpenSqlConn();
            using(SqlCommand command = conn.CreateCommand())
            {
                command.CommandText = sql;
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(retTable);
            }

            return retTable;
        }

        internal static bool ExecuteNonQuery(Model.ConnectionConext connCtx, string sql)
        {
            SqlConnection conn = connCtx.GetOpenSqlConn();
            using (SqlCommand command = conn.CreateCommand())
            {
                command.CommandText = sql;
                int affectRows = command.ExecuteNonQuery();

                return affectRows > 0 ? true : false;
            }
        }

        internal static object ExecuteScalar(Model.ConnectionConext connCtx, string sql)
        {
            SqlConnection conn = connCtx.GetOpenSqlConn();
            using (SqlCommand command = conn.CreateCommand())
            {
                command.CommandText = sql;
                return command.ExecuteScalar();
            }
        }
    }
}
